package com.feinno.urcs.group.util;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.temporal.ChronoUnit;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

/**
 * @author Li.Hongbo <lihongbo@feinno.com>
 */
public class DateUtils {

    public  static Date getOffset(Date src,long offset,ChronoUnit type){
        return Date.from(LocalDateTime.ofInstant(src.toInstant(), ZoneId.of("UTC")).plus(offset, type).toInstant(ZoneOffset.UTC));

    }
    public static long getUTCDate(Date dt) {
        Calendar cal = GregorianCalendar.getInstance();
        // 2、取得时间偏移量：
        int zoneOffset = cal.get(java.util.Calendar.ZONE_OFFSET);

        // 3、取得夏令时差：
        int dstOffset = cal.get(java.util.Calendar.DST_OFFSET);

        // 之后调用cal.get(int x)或cal.getTimeInMillis()方法所取得的时间即是UTC标准时间。
        return dt.getTime()-(zoneOffset + dstOffset);
    }

    public static Date getUTCNow(){
        return new Date(getUTCDate(new Date()));

    }
}
